package com.mbc.rmt.manager;

import java.sql.SQLException;
import java.util.HashMap;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRResultSetDataSource;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.util.JRLoader;

import com.mbc.common.CommonSingleton;
import com.mbc.common.dao.CommonDAOFactory;
import com.mbc.common.dao.LogDAO;
import com.mbc.common.entities.Log;
import com.mbc.common.exception.DataDuplicationException;
import com.mbc.common.manager.AbstractMgr;
import com.mbc.common.utils.KeyGenerator;
import com.mbc.rmt.dao.OfficeCOADAO;
import com.mbc.rmt.dao.RemittanceDAOFactory;
import com.mbc.rmt.data.OfficeCOADataList;
import com.mbc.rmt.entities.OfficeCOA;
import com.mbc.rmt.entities.OfficeCOARpt;


public class OfficeCOAMgr extends AbstractMgr {
	
	private OfficeCOADAO officeCOADAO;
	private LogDAO logDAO;
	
	public OfficeCOAMgr(){}
	
	public OfficeCOAMgr(String sessionId){
		this.sessionId = sessionId;
	}
	public void create(OfficeCOA entity) throws Exception {
		openConnection();
		try{
			officeCOADAO = RemittanceDAOFactory.getOfficeCOADAO(dbManager.getConnection());
			entity.setOffice_coa_id(KeyGenerator.generateKey(getConnection()));
			officeCOADAO.create(entity);
			
			logDAO = CommonDAOFactory.getLogDAO(getConnection());
			Log l_logentity = new Log();
			l_logentity.setTablename("OfficeCOA");
			l_logentity.setLogkey(KeyGenerator.generateKey(getConnection()));
			l_logentity.setKey(entity.getOffice_coa_id());
			l_logentity.setUserid(entity.getUserid());
			l_logentity.setLogstatus(Log.__SAVE_DATA);
			logDAO.getData(l_logentity);
			
		} catch (SQLException e){
			if(DataDuplicationException.UNIQUE_KEY_ERROR_CODE == e.getErrorCode())
				throw new DataDuplicationException(e.getMessage());
			else
				throw e;
		} finally {
			closeConnection();
		}
	}
	public void update (OfficeCOA entity) throws Exception {
		openConnection();
		try{
			officeCOADAO = RemittanceDAOFactory.getOfficeCOADAO(dbManager.getConnection());
			officeCOADAO.update(entity);
			
			logDAO = CommonDAOFactory.getLogDAO(getConnection());
			Log l_logentity = new Log();
			l_logentity.setTablename("OfficeCOA");
			l_logentity.setLogkey(KeyGenerator.generateKey(getConnection()));
			l_logentity.setKey(entity.getOffice_coa_id());
			l_logentity.setUserid(entity.getUserid());
			l_logentity.setLogstatus(Log.__UPDATE_DATA);
			logDAO.getData(l_logentity);
			
		} catch (Exception e){
			throw e;
		} finally {
			closeConnection();
		}
	}

	public void delete (long key,long userid) throws Exception {
		openConnection();
		try{
			officeCOADAO = RemittanceDAOFactory.getOfficeCOADAO(dbManager.getConnection());
			
			logDAO = CommonDAOFactory.getLogDAO(getConnection());
			Log l_logentity = new Log();
			l_logentity.setTablename("OfficeCOA");
			l_logentity.setLogkey(KeyGenerator.generateKey(getConnection()));
			l_logentity.setKey(key);
			l_logentity.setUserid(userid);
			l_logentity.setLogstatus(Log.__DELETE_DATA);
			logDAO.getData(l_logentity);
			
			officeCOADAO.delete(key);
		} catch (Exception e){
			throw e;
		} finally {
			closeConnection();
		}
	}
	public OfficeCOA findByCode (String code, long branchID) throws Exception {
		openConnection();
		try{
			officeCOADAO = RemittanceDAOFactory.getOfficeCOADAO(dbManager.getConnection());
			return officeCOADAO.findByCode(code,branchID);
		} catch (Exception e){
			throw e;
		} finally {
			closeConnection();
		}
	}
	public OfficeCOADataList findOfficeCOA(OfficeCOA cri)throws Exception {
		openConnection();
		try{
			officeCOADAO = RemittanceDAOFactory.getOfficeCOADAO(dbManager.getConnection());
			return officeCOADAO.findOfficeCOA(cri);
		} catch (Exception e){
			throw e;
		} finally {
			closeConnection();
		}
	}
	public OfficeCOADataList getOfficeCOAList(OfficeCOARpt office)throws Exception{
		openConnection();
		try {
			officeCOADAO = RemittanceDAOFactory.getOfficeCOADAO(dbManager.getConnection());
			return officeCOADAO.getOfficeCOAList(office);
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}
	}
	public JasperPrint prepareOfficeCOAReport(OfficeCOARpt officeCOA) throws Exception {
		openConnection();
		JasperPrint l_jsPrint = null;
		try {
			officeCOADAO = RemittanceDAOFactory.getOfficeCOADAO(getConnection());
//			ResultSet l_rs = OfficeCOADAOImpls.prepareOfficeCOAReport(fromofficecoa,toofficecoa ,getConnection());

			JRResultSetDataSource l_jrRS = new JRResultSetDataSource(officeCOADAO.prepareOfficeCOAReport(officeCOA));
		
			 JasperReport l_jsRpt =(JasperReport)JRLoader.loadObjectFromFile(CommonSingleton.ABSOLUTE_PATH +"reports/OfficeCOA.jasper");
		
			 HashMap<String, Object> l_params = new HashMap<String, Object>();
				l_params.put("companyname", "mbc");
			l_jsPrint = JasperFillManager.fillReport(l_jsRpt, l_params, l_jrRS);

		} catch (JRException je) {
			throw je;
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}

		return l_jsPrint;

	}
	
}
